package cn.itrip.controller;

import cn.itrip.pojo.Page;
import cn.itrip.service.SearchHotelService;
import cn.itrip.utils.EmptyUtils;
import cn.itrip.vo.ItripHotelDTO;
import cn.itrip.vo.ItripHotelPageDTO;
import cn.itrip.vo.MyPageDTO;
import cn.itrip.vo.R;
import cn.itrip.vo.seach.SearchHotCityVO;
import cn.itrip.vo.seach.SearchHotelVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/hotellist")
public class HotelListController {

    @Resource
    private SearchHotelService searchHotelService;

    @PostMapping("/searchItripHotelListByHotCity")
    public R searchItripHotelListByHotCity(@RequestBody SearchHotCityVO vo) throws Exception {
        if (EmptyUtils.isEmpty(vo) || EmptyUtils.isEmpty(vo.getCityId())) {
            R r = new R();
            r.setErrorCode("20004");
            r.setMsg("城市id不能为空");
            r.setSuccess("false");
            return r;
        }
        Map<String, Object> param = new HashMap<>(5);
        param.put("cityId", vo.getCityId());
        List list = searchHotelService.searchItripHotelListByHotCity(vo.getCityId(), vo.getCount());
        return R.success(null,list);
    }

    @PostMapping("/searchItripHotelPage")
    public R searchItripHotelPage(@RequestBody SearchHotelVO vo) {
        Page page = null;
        MyPageDTO myPage = new MyPageDTO();
        if (EmptyUtils.isEmpty(vo) || EmptyUtils.isEmpty(vo.getDestination())) {
            R r = new R();
            r.setErrorCode("20002");
            r.setMsg("目的地不能为空");
            r.setSuccess("false");
            return r;
        }
        try {
            page = searchHotelService.searchItripHotelPage(vo, vo.getPageNo(), vo.getPageSize());
            myPage.setBeginPos(page.getBeginPos());
            myPage.setCurPage(page.getCurPage());
            myPage.setPageCount(page.getPageCount());
            myPage.setPageSize(page.getPageSize());
            myPage.setTotal(page.getTotal());
            List<ItripHotelPageDTO> itripHotelPageDTOS = new ArrayList<>();
            for (int i = 0;i<page.getRows().size();i++){
                ItripHotelPageDTO pageDTO = new ItripHotelPageDTO();
                ItripHotelDTO itripHotelDTO = (ItripHotelDTO) page.getRows().get(i);
                pageDTO.setId(itripHotelDTO.getId());
                pageDTO.setHotelName(itripHotelDTO.getHotelName().get(0));
                pageDTO.setAddress(itripHotelDTO.getAddress().get(0));
                pageDTO.setHotelLevel(itripHotelDTO.getHotelLevel());
                pageDTO.setRedundantCityName(itripHotelDTO.getRedundantCityName().get(0));
                pageDTO.setRedundantProvinceName(itripHotelDTO.getRedundantProvinceName().get(0));
                pageDTO.setRedundantCountryName(itripHotelDTO.getRedundantCountryName().get(0));
                pageDTO.setMaxPrice(itripHotelDTO.getMaxPrice());
                pageDTO.setMinPrice(itripHotelDTO.getMinPrice());
                pageDTO.setExtendPropertyNames(itripHotelDTO.getExtendPropertyNames().get(0));
                pageDTO.setExtendPropertyPics(itripHotelDTO.getExtendPropertyPics().get(0));
                pageDTO.setTradingAreaNames(itripHotelDTO.getTradingAreaNames().get(0));
                pageDTO.setFeatureNames(itripHotelDTO.getFeatureNames().get(0));
                pageDTO.setIsOkCount(itripHotelDTO.getIsOkCount());
                pageDTO.setCommentCount(itripHotelDTO.getCommentCount());
                pageDTO.setAvgScore(itripHotelDTO.getAvgScore());
                pageDTO.setImgUrl(itripHotelDTO.getImgUrl().get(0));
                itripHotelPageDTOS.add(pageDTO);
            }
            myPage.setRows(itripHotelPageDTOS);
        } catch (Exception e) {
            e.printStackTrace();
            R r = new R();
            r.setErrorCode("20001");
            r.setMsg("系统异常,获取失败");
            r.setSuccess("false");
            return r;
        }
        return R.success(null,myPage);
    }
}
